#include "xTreeModel.h"
#include <QIcon>
#include "qcomm.h"
#include "xTreeCheckManager.h"

#ifndef TREE_ITEM_ICON_Project
#define TREE_ITEM_ICON_Project QStringLiteral("treeItem_Project")
#endif
#ifndef TREE_ITEM_ICON_folder
#define TREE_ITEM_ICON_folder QStringLiteral("treeItem_folder")
#endif
#ifndef TREE_ITEM_ICON_folderOriginal
#define TREE_ITEM_ICON_folderOriginal QStringLiteral("treeItem_folder-ansys")
#endif
#ifndef TREE_ITEM_ICON_folderAnsys
#define TREE_ITEM_ICON_folderAnsys QStringLiteral("treeItem_folder-ansys")
#endif
#ifndef TREE_ITEM_ICON_TdmsGroup
#define TREE_ITEM_ICON_TdmsGroup QStringLiteral("treeItem_group")
#endif
#ifndef TREE_ITEM_ICON_TdmsChannel
#define TREE_ITEM_ICON_TdmsChannel QStringLiteral("treeItem_channel")
#endif
#ifndef TREE_ITEM_ICON_DataItem
#define TREE_ITEM_ICON_DataItem QStringLiteral("treeItem_dataItem")
#endif

xTreeModel::xTreeModel(QObject *parent) : QStandardItemModel(parent)
{   
    QStringList list;
    list.push_back("文件结构");
    setHorizontalHeaderLabels(list);

    //初始化icon
    m_IconMap[TREE_ITEM_ICON_Project] = QIcon(QStringLiteral("../res_treeItemIcon/Project.png"));
    m_IconMap[TREE_ITEM_ICON_folder] = QIcon(QStringLiteral("../res_treeItemIcon/folder.png"));
    m_IconMap[TREE_ITEM_ICON_folderAnsys] = QIcon(QStringLiteral("../res_treeItemIcon/folder-ansys.png"));
    m_IconMap[TREE_ITEM_ICON_TdmsGroup] = QIcon(QStringLiteral("../res_treeItemIcon/group.png"));
    m_IconMap[TREE_ITEM_ICON_TdmsChannel] = QIcon(QStringLiteral("../res_treeItemIcon/channel.png"));
    m_IconMap[TREE_ITEM_ICON_folderOriginal] = QIcon(QStringLiteral("../res_treeItemIcon/folder_original.png"));
    m_IconMap[TREE_ITEM_ICON_DataItem] = QIcon(QStringLiteral("../res_treeItemIcon/dataItem.png"));

    m_pCheckManager = xTreeCheckManager::getInstance();
    //关联项目属性改变的信号和槽
    connect(this, &QStandardItemModel::itemChanged, m_pCheckManager, &xTreeCheckManager::treeItemChanged);
}

//==============================================================================

xTreeModel::~xTreeModel()
{

}

void xTreeModel::addSceneData(QStringList file)
{
    if(file.size() != 2)
    {
        return;
    }

    QStandardItem* itemFilePath = new QStandardItem(m_IconMap[TREE_ITEM_ICON_folder], file[0]);

    //是否已经存在目录
    QList<QStandardItem*> listItem = findItems(file[0], Qt::MatchExactly);

    //如果目录是相同的
    if(listItem.size() != 0)
    {
        QStandardItem* itemFileTitle = new QStandardItem(m_IconMap[TREE_ITEM_ICON_DataItem], file[1]);
        listItem[0]->appendRow(itemFileTitle);

        m_pCheckManager->setItemChecked(itemFileTitle);
        bool bIsAllCheck = m_pCheckManager->isAllChildChecked(listItem[0]);

        //设置部分check
        if(!bIsAllCheck)
        {
            m_pCheckManager->setItemPartialChecked(listItem[0]);
        }
        else
        {
            m_pCheckManager->setItemChecked(listItem[0]);
        }

        //发消息进行展开
        QModelIndex mIndex = this->indexFromItem(listItem[0]);
        sigExpandItem(mIndex);

    }
    else        //如果目录不相同的
    {
        appendRow(itemFilePath);
        m_pCheckManager->setItemChecked(itemFilePath);
        itemFilePath->setTristate(true);

        QStandardItem* itemFileTitle = new QStandardItem(m_IconMap[TREE_ITEM_ICON_DataItem], file[1]);
        itemFilePath->appendRow(itemFileTitle);

        m_pCheckManager->setItemChecked(itemFileTitle);

        //发消息进行展开
        QModelIndex mIndex = this->indexFromItem(itemFilePath);
        sigExpandItem(mIndex);
    }
}

void xTreeModel::removeAllData()
{
    int row = rowCount();
    removeRows(0, row);
    reset();
}

void xTreeModel::setCheck(int row, int col)
{
    reset();
}


void xTreeModel::reset()
{
    // 刷新模型
    beginResetModel();
    endResetModel();
}







